<%--
    Document   : index
    Created on : 09 Apr 11, 0:21:47
    Author     : asus
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="database.db" %>
<%@page import="bean.*" %>
<%@page import="chat.*" %>
<%@page import="java.util.ArrayList" %>
<%
    db.setConnection();
    ArrayList<category> c = db.getcategories();
    ArrayList<user> u = db.getfriendlist(Integer.parseInt(session.getAttribute("uid").toString()));

    String uid = (session.getAttribute("uid")).toString();
    String uname = (session.getAttribute("username")).toString();

    ChatRoomList roomList = (ChatRoomList)application.getAttribute("roomList");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../css/style.css"/>
        <script type="text/javascript" src="../js/validator.js"></script>
        <script type="text/javascript" src="../js/chat.js"></script>
        <title>popoChat</title>
        <title>User</title>
    </head>

    <body>
        <div class="main_wrapper">
            <div class="login">
                <div class="logininput">
                    <div class="logout">
                        <a href="../logout"><img src="../image/exit.png" alt="logout" id="logout"/></a>
                        logout
                        <% if (uname != null) { %>
                        <div id="activeUsername">
                            <label>You are logged in as <b><%= uname %></b></label>
                        </div>
                        <% } %>
                    </div>
                </div>
            </div>

            <div class="user_content">
                <div class="left_area">
                    <div id="friendslist">

                        My Friends : 
                        <select class="idropdown" name="friends" multiple="true">
                            <% for(int i=0; i<u.size(); i++){ %>
                            <option 
                                onclick='<% session.setAttribute("selectedRoom",Integer.parseInt(application.getAttribute("globalRoomCounter").toString())+1); %>createNewTab("<%= session.getAttribute("username") %>","<%= u.get(i).getUsername() %>", <%= Integer.parseInt(application.getAttribute("globalRoomCounter").toString())+1 %>)'
                                value="<%= u.get(i).getId() %>"><%= u.get(i).getUsername() %>
                            </option>
                            <% } %>
                        </select>

                    </div>
                    <div id="rooms">
                        <!--dummy-->
                        Rooms :
                        <select class="idropdown" name="rooms" multiple="true">
                            <% for(int i=0; i<c.size(); i++){ %>
                            <option value="<%= c.get(i).getId() %>"><%= c.get(i).getName() %></option>
                            <% } %>
                        </select>
                    </div>
                </div>
                <div class="right_area">
                    <table id="myChatTable">
                        <tr>
                            <div id="myChatBoxHeader">
                                myChatBox
                            </div>
                        </tr>
                        <tr>
                            <div id="myChatTab">
                                <!--isi tab2 room & chat yg kbuka harusnya disini ntar 
                                iterasi setiap tab yg ada
                                DIGENERATE TIAP DI KLIK BARU
                                -->
                                <%
                                    roomList = (ChatRoomList)application.getAttribute("roomList");
                                    ChatRoom[] roomsArray = roomList.getRoomListArray();
                                    for (int a = 0;a < roomsArray.length;a++){
                                        //if (roomsArray[a].chatterExists(session.getAttribute("username").toString())){
                                            %>
                                            <div class='contentChatTab'>
                                            <a onclick=" <% session.setAttribute("selectedRoom",roomsArray[a].getId()); %> " href=""><%= roomsArray[a].getName() %></a>
                                            <a onclick="deleteTab(<%= session.getAttribute("selectedRoom") %>)" href=""><img src="../image/delete.png" alt="delete" height="10px" width="10px"></a>
                                            </div>
                                        <%
                                        //}
                                    }
                                %>
                                <div class="clear"></div>
                            </div>
                        </tr>
                        <tr>
                            <div id="myContentChat">
                                <!--daftar user dalam room harusnya disini ntar -->
                                <div id="chatArea">
                                    <%
                                    /*ChatRoom roomChat = roomList.getRoomOfId(Integer.parseInt(session.getAttribute("selectedRoom").toString()));
                                    /*
                                    Load Room's Chat Message
                                    */
                                    /*out.println("tes");
                                    Message[] roomsMessages = roomChat.getMessages();
                                    for (int a = 0; a < roomsMessages.length; a++){
                                        out.println("<div>" + roomsMessages[a].getChatterName() + " : " + roomsMessages[a].getMessage() + "<br></div>");
                                    }*/
                                    %>
                                    <script type="text/javascript">
                                        initAjax(<%= Integer.parseInt(session.getAttribute("selectedRoom").toString()) %>);
                                    </script>
                                </div>
                                <div id="myRoomUser">
                                    <!--daftar user dalam room harusnya disini ntar -->
                                    <% 
                                        if (roomList.getRoomOfId(Integer.parseInt(session.getAttribute("selectedRoom").toString()))!=null){
                                        Chatter[] inRoom = roomList.getRoomOfId(Integer.parseInt(session.getAttribute("selectedRoom").toString())).getChattersArray();
                                        for (int a = 0;a< inRoom.length;a++){
                                    %>
                                    <div class="roomUser" id=""><a onclick="" href=""><%= inRoom[a].getName() %></a></div>
                                    <% }
                                    }
                                    %>
                                </div>
                                <div class="clear"></div>
                            </div>
                        </tr>
                        <tr>
                            <div id="postToChat">
                                <!--disini onchangenya diganti sama ajax baru -->
                                <input type="text" class="itext2" id="ChattoPost" name="ChattoPost" onchange="postMsg(document.getElementById('ChattoPost').value, <%= Integer.parseInt(session.getAttribute("selectedRoom").toString()) %>)"/>
                            </div>
                        </tr>
                    </table>
                </div>
                <div class="clear"></div>
            </div>

            <div id="footer">
                Created by Ana - Edwin - Athia
            </div>
        </div>
    </body>
</html>
